package com.lanou.zy.projecta.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.entity.SearchResult;
import com.lanou.zy.projecta.util.OnItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/11/4.
 */
public class SearchResultRecyclerViewAdapter extends RecyclerView.Adapter<SearchResultRecyclerViewAdapter.SearchREsultViewHolder>{
    private List<SearchResult.DataEntity.EntryEntity> entities;
    private OnItemClickListener onItemClickListener;

    public void addData(List<SearchResult.DataEntity.EntryEntity> datas) {
        this.entities = datas;
        notifyDataSetChanged();
    }

    @Override
    public SearchREsultViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.search_result_recycler_view_item, null);
        return new SearchREsultViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final SearchREsultViewHolder holder, int position) {
        if (entities != null && entities.size() > 0) {
            SearchResult.DataEntity.EntryEntity sde = entities.get(position);
            holder.searchResultTv.setText(sde.getCnname());
        }

        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    onItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.onItemClickListener = itemClickListener;
    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }

    public class SearchREsultViewHolder extends RecyclerView.ViewHolder {
        private TextView searchResultTv;

        public SearchREsultViewHolder(View v) {
            super(v);
            searchResultTv = (TextView) v.findViewById(R.id.searchResultTv);
        }
    }
}
